package com.sgb.goods.widget.pop.adapter;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.entity.GoodsBrandEntity;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述：品牌弹窗-品牌列表适配器
 *
 * @author qiangHeNoo
 * <p>
 * 时间：2021.12.09
 */
public class GoodsBrandAdapter extends BaseRecycleAdapter<GoodsBrandEntity> {

    private GoodsBrandChildAdapter brandChildAdapter;
    private ISelectChild iSelectChild;
    private boolean isUpdate = true;
    private final List<GoodsBrandChildAdapter> brandChildAdapterList = new ArrayList<>();

    public GoodsBrandAdapter(Context context, List<GoodsBrandEntity> data) {
        super(context, R.layout.goods_item_brand1, data);

    }

    @Override
    public void convert(ItemViewHolder holder, final GoodsBrandEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        TextView brandTv = holder.getItem().findViewById(R.id.goods_brand_title);
        RecyclerView childList = holder.itemView.findViewById(R.id.rv_brand);
        brandTv.setText(entity.getName());
        if (entity.getChildList() != null && entity.getChildList().size() > 0) {
            brandTv.setVisibility(View.VISIBLE);
            childList.setVisibility(View.VISIBLE);
        } else {
            brandTv.setVisibility(View.GONE);
            childList.setVisibility(View.GONE);
        }
        if (isUpdate) {
            brandChildAdapter = new GoodsBrandChildAdapter(context, null);
            brandChildAdapter.setItemSelect(true);
            childList.setLayoutManager(new LinearLayoutManager(context));
            childList.setAdapter(brandChildAdapter);
            brandChildAdapterList.add(brandChildAdapter);
        }
        brandChildAdapter.setOnItemClick(new AdapterOnItemClick<GoodsBrandEntity.GoodsBrandChild>() {
            @Override
            public void onItemClick(GoodsBrandEntity.GoodsBrandChild brandChild, int position) {
                iSelectChild.select(brandChild.getName(), brandChild.getId());
                for (int i = 0; i < brandChildAdapterList.size(); i++) {
                    if (holder.getLayoutPosition() != i) {
                        brandChildAdapterList.get(i).resetCheckDatas();
                        brandChildAdapterList.get(i).setDefItem(-1);
                    }
                }
            }
        });
        brandChildAdapter.setDatas(datas.get(holder.getLayoutPosition()).getChildList());
    }

    public interface ISelectChild {
        /**
         * 品牌选中回调
         *
         * @param name 品牌名称
         * @param id   名称对应iD
         */
        void select(String name, String id);
    }

    public void iSelectChild(ISelectChild iSelectChild) {
        this.iSelectChild = iSelectChild;
    }

    public void isUpdate(boolean isUpdate) {
        this.isUpdate = isUpdate;
        if (isUpdate) {
            brandChildAdapterList.clear();
        }
    }
}
